package com.cjf.androidstudy.component;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

import com.cjf.androidstudy.R;

/**
 * author: cjf
 * date: 2021-07-13 22:22
 * description: 动态广播，监听网络切换变化---操作：网络切换飞行模式，就会弹一条toast
 */
public class BroadcastReceiverActivity extends AppCompatActivity {

    private static final String TAG = "BroadcastRcrActivity";
    /**
     * 用完需要取消注册
     */
    private NetworkChangeReceiver networkChangeReceiver;

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast_receiver);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);


        //    OkHttpHelper.getRequest("https://www.baidu.com", new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.e(TAG, "onFailure: ",e );
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                Log.i(TAG, "onResponse: " + response.body().string());
//            }
//        });
//
//        String url = "https://www.recaptcha.net/recaptcha/api2/reload?k=6LfFqK0UAAAAAAJUJQOnlNcN1hOL_7v4_33LQxGm";
//        FormBody.Builder builder = new FormBody.Builder();
//        OkHttpHelper.postRequest(url, builder, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.e(TAG, "Post-onFailure: ",e);
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                Log.i(TAG, "Post-onResponse: " + response.body().string());
//            }
//        });
        webView = findViewById(R.id.webview1);
        //如果只写下面这一句，会提示无法访问
        //加载网页需要连接互联网的权限，需要在AndroidManifest中进行声明
        //这样的好处：可以让用户很清楚的看到app所需要的权限
        //用户并不清楚和重视权限问题，造成了病毒问题的泛滥。其实是可以避免的！
        webView.loadUrl("https://blog.csdn.net/weixin_30877755/article/details/99263427");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("BroadcastReceiverActivity onResume=================");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                System.out.println("延时任务已启动");
            }
        }, 10000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
    }

    class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "network change");
            Toast.makeText(context, "动态广播-network change", Toast.LENGTH_SHORT).show();
        }
    }
}
